package com.test.study.ToolsUtils.strom.workdsum;

import java.util.HashMap;
import java.util.Map;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

@SuppressWarnings("serial")
public class GetWordCount extends BaseRichBolt {
	
@SuppressWarnings({ "unused", "rawtypes" })
private Map stormConf;
@SuppressWarnings("unused")
private TopologyContext context;
@SuppressWarnings("unused")
private OutputCollector collector;

	@SuppressWarnings("rawtypes")
	@Override
	public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
		this.stormConf=stormConf;
		this.context=context;
		this.collector=collector;

	}
	HashMap<String,Integer> map=new HashMap<String,Integer>();

	@Override
	public void execute(Tuple input) {
		String stringByField = input.getStringByField("word");
		if(stringByField!=null && stringByField.length()>0) {
			Integer integer = map.get(stringByField);
			if(integer==null) {
				integer=0;
			}
			integer++;
			map.put(stringByField, integer);
			this.collector.emit(new Values(stringByField,integer));
			
		}
	
	}

	@Override
	public void declareOutputFields(OutputFieldsDeclarer declarer) {
		declarer.declare(new Fields("words","count"));
	

	}

}
